#-*- coding:utf-8 -*-
import os
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

class CreateEpi(QWidget):
    epi_list_Signal=Signal(list)

    def __init__(self,real_department_path):
        super(CreateEpi,self).__init__()
        self.setStyleSheet(
            'QWidget{background-color:rgb(75,75,75);color:rgb(255,127,80)}QPushButton{background-color:rgb(100,100,100);border:2px;border-radius:2px;padding:3px}QPushButton:hover{color:#FFC0CB}')

        self.setWindowTitle('创建集/场')
        self.mainLayout=QVBoxLayout()

        self.setMaximumWidth(250)
        self.setMinimumHeight(250)
        #全局变量
        self.current_department_path=real_department_path
        self.epi_list=[]



        self.tab_widget=QTabWidget()



   ###单独创建
        self.single_widget=QWidget()
        # self.single_widget.setStyleSheet('background-color:rgb(100,100,100)')

        self.single_layout=QVBoxLayout()
        self.epi_layout=QHBoxLayout()
        self.epi_label=QLabel('集/场名称:')
        self.epi_edit=QLineEdit()
        self.epi_layout.addWidget(self.epi_label)
        self.epi_layout.addWidget(self.epi_edit)


        self.single_btn_layout=QHBoxLayout()
        self.single_btn_layout.setSpacing(20)
        self.single_OK_btn=QPushButton('创建')
        self.single_OK_btn.clicked.connect(self.singleCreate)
        self.single_NO_btn=QPushButton('取消')
        self.single_NO_btn.clicked.connect(self.closeWindow)
        self.single_btn_layout.addWidget(self.single_OK_btn)
        self.single_btn_layout.addWidget(self.single_NO_btn)

        self.single_layout.addLayout(self.epi_layout)
        self.single_layout.addLayout(self.single_btn_layout)

        self.single_widget.setLayout(self.single_layout)


    ####批量创建
        self.batch_widget=QWidget()

        self.batch_layout=QVBoxLayout()
        self.batch_layout.setSpacing(20)
        self.preName_layout=QHBoxLayout()
        self.preName_label=QLabel('名称前缀:')
        self.preName_edit=QLineEdit()
        self.preName_edit.setPlaceholderText('__可以为空')
        self.preName_layout.addWidget(self.preName_label)
        self.preName_layout.addWidget(self.preName_edit)

        self.range_layout=QHBoxLayout()
        self.range_label=QLabel('集/场范围:')
        self.range_start_edit=QLineEdit()
        intlimit=QIntValidator(0,9999,self)
        self.range_start_edit.setValidator(intlimit)
        self.range_TO_label=QLabel('-')
        self.range_end_edit=QLineEdit()
        self.range_end_edit.setValidator(intlimit)
        self.range_layout.addWidget(self.range_label)
        self.range_layout.addWidget(self.range_start_edit)
        self.range_layout.addWidget(self.range_TO_label)
        self.range_layout.addWidget(self.range_end_edit)

        self.batch_btn_layout=QHBoxLayout()
        self.batch_OK_btn=QPushButton('创建')
        self.batch_OK_btn.clicked.connect(self.batchCreate)
        self.batch_NO_btn=QPushButton('取消')
        self.batch_NO_btn.clicked.connect(self.closeWindow)
        self.batch_btn_layout.addWidget(self.batch_OK_btn)
        self.batch_btn_layout.addWidget(self.batch_NO_btn)

        self.batch_layout.addLayout(self.preName_layout)
        self.batch_layout.addLayout(self.range_layout)
        self.batch_layout.addLayout(self.batch_btn_layout)

        self.batch_widget.setLayout(self.batch_layout)






        self.tab_widget.addTab(self.single_widget,'单独创建')
        self.tab_widget.addTab(self.batch_widget,'批量创建')

        self.mainLayout.addWidget(self.tab_widget)
        self.setLayout(self.mainLayout)



    def singleCreate(self):
        '''
        单独创建集数
        :return:
        '''
        epi_name=self.epi_edit.text()
        if epi_name:
            epi_list=[epi_name]
            self.__createEpi(epi_list)
        else:
            QMessageBox.information(self, '提示', '请输入集/场名称')



    def batchCreate(self):
        '''
        批量创建
        :return:
        '''
        pre_name=self.preName_edit.text()
        start_text=self.range_start_edit.text()
        end_text=self.range_end_edit.text()

        epi_name_list=[]

        if start_text and end_text:
            start = int(start_text)
            end = int(end_text)
            for i in range(start,end):
                str_i=self.intToStr(i)
                epi_name=pre_name+str_i
                epi_name_list.append(epi_name)

            print(epi_name_list)
            self.__createEpi(epi_name_list)

        else:
            QMessageBox.information(self, '提示', '请输入完整信息')






    def __createEpi(self,epi_name_list):
        '''
        计算函数，用来创建集数目录
        :param epi_name_list:
        :return:
        '''
        # print(self.current_department_path)
        # print(epi_name_list)
        current_epi_list = os.listdir(self.current_department_path)

        exist_epi_info=''
        temp_list=current_epi_list
        for i in epi_name_list:
            if i not in current_epi_list:
                epi_path = os.path.join(self.current_department_path, i)
                # print(epi_path)
                os.makedirs(epi_path)
                temp_list.append(i)
            else:
                exist_epi_info+=i+' '

        QMessageBox.information(self, '提示', '创建成功')
        if exist_epi_info !='':
            QMessageBox.information(self,'提示','{}已经创建'.format(exist_epi_info))

        self.epi_list =temp_list
        self.epi_list_Signal.emit(self.epi_list)
        self.close()
        print(self.epi_list)

    def intToStr(self,num):
        if num<10:
            return '0'+str(num)
        else:
            return str(num)

    def closeWindow(self):
        self.close()






if __name__ == '__main__':
    app=QApplication(sys.argv)
    ce=CreateEpi('../Sample/Projects/狮子王/动画')
    ce.show()
    sys.exit(app.exec_())




